#include<bits/stdc++.h>
using namespace std;

struct node{
    int t;
    string p,v;
};

void solve(){
    int n,a,b;
    cin>>n>>a>>b;
    int n1=0,a1=0,b1=0;
    int s;
    cin>>s;
    vector<node> ss;
    vector<int> cnt(20,0);
    vector<int> vis(20,0);
    vector<int> pd;
    while(s--){
        int t;
        string p,v;
        cin>>t>>p>>v;
        ss.push_back({t,p,v});
    }
    for(int i=0;i<ss.size();i++){
        if(ss[i].t<240){
            if(vis[ss[i].p[0]-'A']==1)continue;
            if(ss[i].v=="ac"){
                vis[ss[i].p[0]-'A']=1;
                b1+=ss[i].t+cnt[ss[i].p[0]-'A']*20;
                a1++;
            }else{
                cnt[ss[i].p[0]-'A']++;
            }
        }else{
            if(vis[ss[i].p[0]-'A']==1)continue;
            int tim=ss[i].t+cnt[ss[i].p[0]-'A']*20;
            vis[ss[i].p[0]-'A']=1;
            pd.push_back(tim);
        }
    }
    sort(pd.begin(),pd.end());
    if(a1>a||(a1==a&&b1<b)){
        cout<<0<<'\n';
        return;
    }
    for(int i=0;i<pd.size();i++){
        a1++;
        b1+=pd[i];
        if(a1>a||(a1==a&&b1<b)){
            cout<<i+1<<'\n';
            return;
        }
    }
    cout<<-1<<'\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int _=1;
    cin>>_;
    while(_--){
        solve();
    }
    return 0;
}